package com.exam.controller;

import com.exam.dto.ActionResult;
import com.exam.po.ExamManage;
import com.exam.service.AdminExamService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.AccessibleObject;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName AdminExamController
 * @Description TODO
 * @action luoliang
 * @Data 2021/1/13 10:27
 * @Version 1.0
 */
@RequestMapping("/api")
@RestController
public class AdminExamController {

    @Autowired
    private AdminExamService adminExamService;

    /**
     * 分页查考试
     * @param current
     * @param size
     * @return
     */
    @ApiOperation(value = "分页查询考试信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "查询的页码", required = true),
            @ApiImplicitParam(name = "size", value = "页面数据容量", required = true)
    })
    @RequestMapping(value = "/findexam/{current}/{size}",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult FindBypage(@PathVariable Integer current,@PathVariable Integer size){
        //System.out.println(size);
        HashMap<String, Object> map = new HashMap<>();
        int total = adminExamService.countExample();
        //System.out.println(total);
        List<ExamManage> list = adminExamService.findAdminBypage(current, size);

        map.put("total",total);
        map.put("data",list);
        //System.out.println(map+"================");

        if (map != null){
            ActionResult success = new ActionResult(200, "success", map);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);
            return error;
        }
    }

    /**
     * 查询所有不分页
     * @return
     */
    @RequestMapping(value = "/Adminexams",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult findAdminExam(){
        List<ExamManage> list = adminExamService.selectAdminExam();
        ActionResult success = new ActionResult(200, "success", list);
        return success;
    }
    @RequestMapping(value = "/examManagePaperId",method = RequestMethod.GET)
    public ActionResult FindExamOnly(){
        ExamManage examManage = adminExamService.selectOnlyPaperId();
        ActionResult success = new ActionResult(200, "success", examManage);
        return success;
    }
    /**
     * 添加考试
     * @param examManage
     * @return
     */
    @RequestMapping(value = "/addexam",method = RequestMethod.POST)
    public ActionResult AddAdminExam(@RequestBody ExamManage examManage){
        int i = adminExamService.AddSelective(examManage);

        if (i>0){
            ActionResult success = new ActionResult(200, "success", i);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);
            return error;
        }
    }

    /**
     * 根据id查找所有
     * @param examcode
     * @return
     */
    @RequestMapping(value = "/findbyid/{examcode}",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult FindByidExam(@PathVariable Integer examcode){
        ExamManage examManage = adminExamService.selectById(examcode);
        ActionResult success = new ActionResult(200, "success", examcode);
        return success;
    }
    /**
     * 修改
     * @param record
     * @return
     */
    @RequestMapping(value = "/updateexam",method = RequestMethod.PUT)
    public ActionResult UpdateAdminExam( @RequestBody ExamManage record){

        int i = adminExamService.updateBySelective(record);


        if (i>-1){
            ActionResult success = new ActionResult(200, "success", i);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);

            return error;
        }
    }

    /**
     * 管理员删除
     * @param examcode
     * @return
     */
    @RequestMapping(value = "/delectexam/{examcode}",method = RequestMethod.DELETE)
    public ActionResult DeleAdminExam(@PathVariable Integer examcode){

        int i = adminExamService.deleteByKey(examcode);
        if (i>-1){
            ActionResult success = new ActionResult(200, "success", i);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);
            return error;
        }
    }

    /**
     * 管理员审核考试
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/adminexam",method = RequestMethod.GET)
    public ActionResult selectExamAdmin(Integer page,Integer size){

        HashMap<String, Object> map = new HashMap<>();
        int total = adminExamService.countAdminExample();

        List<ExamManage> list = adminExamService.findAdminBypage(page, size);

        map.put("total",total);
        map.put("data",list);


        if (map != null){
            ActionResult success = new ActionResult(200, "success", map);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);
            return error;
        }
    }

    /**
     * 管理员审核(已审核)
     * @param
     * @return
     */
    @RequestMapping(value = "/adminweitg/{examcode}",method = RequestMethod.PUT)
    @ResponseBody
    public ActionResult UpdateAdminweitg(@PathVariable Integer examcode){
        int i = adminExamService.updateAdminBySelective(examcode);

        if (i>-1){
            ActionResult success = new ActionResult(200, "success", i);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);
            return error;
        }
    }
    /**
     * 管理员审核(未通过)
     * @param
     * @return
     */
    @RequestMapping(value = "/adminyishenhe/{examcode}",method = RequestMethod.PUT)
    @ResponseBody
    public ActionResult UpdateAdmin(@PathVariable Integer examcode){
        int i = adminExamService.updateAdminYiShenHe(examcode);

        if (i>-1){
            ActionResult success = new ActionResult(200, "success", i);
            return success;
        }else {
            ActionResult error = new ActionResult(203, "error", null);
            return error;
        }
    }
}
